﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Text;
using BusinessLogic.BusinessRules;
using BusinessLogic.ClassFactory;
using System.Data;

namespace RichECMS.news.web.shtml
{
    public partial class ProList : System.Web.UI.Page
    {
        public int panel_count = 0;
        public StringBuilder panel_width = new StringBuilder("[");
        public StringBuilder panel_countsList = new StringBuilder("[");
        public StringBuilder styles = new StringBuilder("[");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["sid"] == null)
            {
                //Response.Redirect("/");
                lithtml.Text = BuildedHtml(5);
            }
            else
            {
                string sid = Request["sid"];
                if (sid == "3" || sid == "4" || sid == "5")
                {
                   lithtml.Text= BuildedHtml(Convert.ToInt32(sid));
                }
            }
        }

        public string BuildedHtml(int intSectionID)
        {
            string strBoubleTemplate = doubletemplate.InnerHtml;
            string strliTemplate = liletemplate.InnerHtml;
            StringBuilder objBuilder = new StringBuilder();
            using (SectionsFactory objSectionsFactory = new SectionsFactory())
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("SELECT *  FROM [Sections] Where ");
                strSql.AppendFormat(" intParentID={0}  Order By intSortNo desc,dtmInTime desc ", intSectionID);

                Sections objSections = objSectionsFactory.CreatorByFullSql(strSql.ToString());
                if (objSections != null)
                {
                    for (int i = 0; i < objSections.Count; i++)
                    {
                        using (ArtSecRelationFactory objArtSecRelationFactory = new ArtSecRelationFactory())
                        {
                            StringBuilder strWhere = new StringBuilder();
                            strWhere.AppendFormat("select  top {0} ArtSecRelation.*,Article.chvTitle ,Article.chvMainMiniPic ,Article.chvFilePath ,Article.dtmInTime from ArtSecRelation,Article where Article.intArticleId=ArtSecRelation.intArticleId and ArtSecRelation.intSecArtID in (select intSecArtID from ArtSecRelation where intSectionID in(", 1000000);

                            strWhere.Append(objSections.DataItem.intSectionID + ",");

                            strWhere.Append(string.Format(" 0000) and ArtSecRelation.intWSID={0}  )  order by  ArtSecRelation.intSort desc,Article.dtmInTime desc", 1));
                            ArtSecRelation objArtSecRelation = objArtSecRelationFactory.NewInstance();
                            DataTable tblTmp = objArtSecRelation.m_Access.ExecuteDataTable(strWhere.ToString());
                            if (tblTmp.Rows.Count > 0)
                            {
                                foreach (DataRow row in tblTmp.Rows)
                                {
                                    if (row["intSectionTop"] != DBNull.Value)
                                    {
                                        string strSREC = row["intSectionTop"].ToString().Trim();
                                        if (strSREC == "1")
                                        {
                                            string imgpath = row["chvFilePath"] != DBNull.Value ? row["chvFilePath"].ToString().Trim() : "";
                                            string imgminipath= row["chvMainMiniPic"] != DBNull.Value ? row["chvMainMiniPic"].ToString().Trim() : "default.jpg";
                                            string trueimhpath = string.IsNullOrEmpty(imgpath) ? imgminipath : "Files/"+imgpath;
                                            objBuilder.Append(strBoubleTemplate.Replace("$Title$", row["chvTitle"].ToString()).Replace("$Aid$", row["intSecArtID"].ToString()).Replace("$PannelIndex$", panel_count.ToString()).Replace("$ImgPath$", trueimhpath));
                                            panel_count = panel_count + 1;
                                            if (panel_width.ToString() == "[")
                                            {
                                                panel_width.Append("2");
                                            }
                                            else
                                            {
                                                panel_width.Append(",2");
                                            }

                                            if (panel_countsList.ToString() == "[")
                                            {
                                                panel_countsList.Append("1");
                                            }
                                            else
                                            {
                                                panel_countsList.Append(",1");
                                            }
                                            break;
                                        }
                                    }
                                }
                                for (int j = 0; j < tblTmp.Rows.Count; j++)
                                {
                                    DataRow row = tblTmp.Rows[j];
                                    if (styles.ToString() == "[")
                                    {
                                        styles.Append("'style_id_" + row["intSecArtID"].ToString() + "'");
                                    }
                                    else
                                    {
                                        styles.Append(",'style_id_" + row["intSecArtID"].ToString() + "'");
                                    }

                                    if (j == 0)
                                    {
                                        objBuilder.Append("<div class=\"ggpanel 0div edge_right\">                <span class=\"panel_index\">" + panel_count + ",</span>    <div class=\"obscure\"></div> <ul id=\"0ul\" class=\"sixup_panel position\">");
                                        panel_count = panel_count + 1;
                                        if (panel_width.ToString() == "[")
                                        {
                                            panel_width.Append("1");
                                        }
                                        else
                                        {
                                            panel_width.Append(",1");
                                        }

                                        if (panel_countsList.ToString() == "[")
                                        {
                                            panel_countsList.Append("6");
                                        }
                                        else
                                        {
                                            panel_countsList.Append(",6");
                                        }
                                    }
                                    else
                                    {
                                        if (j % 6 == 0)
                                        {
                                            objBuilder.Append(" </ul></div><div class=\"ggpanel 0div edge_right\">                <span class=\"panel_index\">" + panel_count + ",</span>    <div class=\"obscure\"></div> <ul id=\"0ul\" class=\"sixup_panel position\">");
                                            panel_count = panel_count + 1;
                                            if (panel_width.ToString() == "[")
                                            {
                                                panel_width.Append("1");
                                            }
                                            else
                                            {
                                                panel_width.Append(",1");
                                            }
                                            if (panel_countsList.ToString() == "[")
                                            {
                                                panel_countsList.Append("6");
                                            }
                                            else
                                            {
                                                panel_countsList.Append(",6");
                                            }
                                        }
                                        //else
                                        //{
                                        //    if (j == tblTmp.Rows.Count - 1)
                                        //    {
                                        //        objBuilder.Append(" </ul></div><div class=\"ggpanel 0div edge_right\">                <span class=\"panel_index\">" + panel_count + ",</span>    <div class=\"obscure\"></div> <ul id=\"0ul\" class=\"sixup_panel position\">");
                                        //    }
                                        //}
                                    }
                                    if (j % 2 == 0)
                                    {
                                        objBuilder.Append(" <li class=\"even\">");
                                    }
                                    else
                                    {
                                        objBuilder.Append(" <li class=\"odd\">");
                                    }

                                    objBuilder.Append(strliTemplate.Replace("$Title$", row["chvTitle"].ToString()).Replace("$Aid$", row["intSecArtID"].ToString()).Replace("$ImgPath$", row["chvMainMiniPic"].ToString()));

                                    objBuilder.Append(" </li>");

                                    if (j == tblTmp.Rows.Count - 1)
                                    {
                                        objBuilder.Append(" </ul></div>");
                                    }
                                }
                            }
                        }
                        objSections.SelectRow(i + 1);
                    }
                }
            }
            styles.Append("]");
            panel_width.Append("]");
            panel_countsList.Append("]");
            return objBuilder.ToString();
        }
    }
}